<?php
namespace app\web\controller;

use app\model\Record; 
use app\model\Theme; 

class LzComment extends Base
{
    public function Lz_comment()
    {
global $_G, $_P;
if (strstr($_SERVER['HTTP_HOST'],".y.yixuan.net")!==false){
    return_msg('请不要使用管理网址操作!');
}

$system = $_G['system'];
$_G['page']['title'] = '粉丝列表 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
if(empty($themeid)){
	return_msg('还没有创建活动,请联系管理员');
}
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
$id = intval($_P['id']);
$do = 'lz_comment';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
if(!isset($_P['type'])){
    $type = -1;
}else{
    $type = intval($_P['type']);
}
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid,'type'=>$type));

$comment_sum['t0'] = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comment ) . "
                WHERE themeid ='{$themeid}' and status = 0 " ));
$comment_sum['t1'] = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comment ) . "
                WHERE themeid ='{$themeid}' and status = 1 " ));
$comment_sum['t2'] = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comment ) . "
                WHERE themeid ='{$themeid}' and status = 2 " ));
$comment_sum['t3'] = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comment ) . "
                WHERE themeid ='{$themeid}' and status = 3 " ));
$comment_sum['t4'] = intval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_comment ) . "
                WHERE themeid ='{$themeid}' and status = 4 " ));
if($op == 'add') {
    if(checksubmit('submit')) {
        $comment_list = trim($_REQUEST['comment']);
        $comment_arr = explode("\r\n", $comment_list);
        $i = 0 ;
        foreach ($comment_arr as $index =>$row){
            if (!empty(trim($row))){
                $i++;
                $data = array(
                    'themeid'     => $themeid,
                    'uid' => '-11',
                    'msg'    => $row,
                    'status'    => 4,
                );
                $insertid = db_insert(t_comment, $data);
            }
        }
        return_tips('成功添加 '.$i." 条评论", $url, 'success');
    }
}
if($op == 'display') {


	$themelist = Theme::getAllTheme();

	$pindex = max(1, intval($_P['page']));
	$psize = 20;


    $condition = '';


	if ($type != -1){
        $condition .= " and c.status=" . $type;
	}

	if(!empty($_P['keyword'])) {
		$condition .= " AND ( c.msg2 LIKE '%" .$_P['keyword'] ."%' or f.mobile='{$_P['keyword']}' or f.nickname LIKE '%{$_P['keyword']}%' or f.username LIKE '%{$_P['keyword']}%') ";
	}

	extract(Record::getSearchTime());

	$condition.= " and c.created_at > '" . date('Y-m-d H:i:s',$starttime) . "' and c.created_at <= '" . date('Y-m-d H:i:s',$endtime) . "' ";
	
	$total = (int)db_fetchcolumn("SELECT COUNT(*) FROM ". tablename(t_comment) . " as c 
	        left join " . tablename (t_fans) ." as f on c.uid = f.uid and c.themeid = f.themeid 
			WHERE 1=1 " . $condition);


    if (strstr($config['softtag0'],"婚纱")!==false ){
        $kaishishu = intval( substr(MODULE_NAME, -3))%30*45+1045;
    }else{
        $kaishishu = intval( substr(MODULE_NAME, -3))%30*45+2545;
    }



    $statustr = array("未审核","审核通过","审核未通过","系统自动屏蔽","手工添加");
	$sql = "SELECT * ,c.status as c_status FROM ". tablename(t_comment) . " as c 
	        left join " . tablename (t_fans) ." as f on c.uid = f.uid and c.themeid = f.themeid 
			WHERE 1=1 " . $condition . "order by c_id desc LIMIT ".($pindex - 1) * $psize.','.$psize;
//	echo $sql;
	$list = db_select($sql);
		foreach ($list as $index =>$row){
            if ($row['c_status']==4){
                $c_id = ($row['c_id'] % 100) +$kaishishu;
                $xunilist = db_find ( "SELECT nickname,avatar FROM " . tablename(t_2019fans) . " where id =". $c_id );

                $list[$index]['nickname']=str_replace("'","",$xunilist['nickname']);
                $list[$index]['avatar'] = $xunilist['avatar'];
                $list[$index]['username'] = '系统虚拟';
            }
		}
	$pager = pagination($total, $pindex, $psize);
	
}

if($op == 'status') {
	$status = intval($_P['status']);
	$info = db_find ( "SELECT * FROM " . tablename ( t_comment ) . " WHERE c_id = " . $_P['c_id']);
    if ($info['status']!=4){
        $state = db_update(t_comment, array('status' => $status,'admin_user' =>$_G['username'],'updated_at'=>date("Y-m-d H:i:s")), array('c_id' => $_P['c_id']));
        return_tips('更新成功', $url, 'success');
    }
}


if($op == 'delete' && $_G['isSuperAdmin']) {
	$uid = intval($_P['uid']);
	db_delete(t_fans, array('uid' => $uid,'themeid'=>$themeid));
	db_delete(t_relation, array('uid' => $uid,'themeid'=>$themeid));
	db_delete(t_scan, array('uid' => $uid,'themeid'=>$themeid));
	return_tips('删除成功', $url, 'success');
}


$return_clicent = get_defined_vars();
return view('/lz_comment' , $return_clicent);
}
}